<template>
  <div class="login_container">
    <div class="login_box">
      <transition
        appear
        name="animate__animated animate__bounce"
        enter-active-class="animate__fadeIn"
        leave-active-class="animate__fadeOut"
      >
        <div class="title">Press management system</div>
      </transition>
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="70px"
        class="login_form"
        status-icon
      >
        <el-form-item label="账户" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入账户"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btns">
          <el-button class="btn" type="primary" @click="login">登录</el-button>
          <el-button class="btn" type="info" @click="resetLoginForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入账户", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        //密码是否合法
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 登录
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "/login",
          qs.stringify(this.loginForm)
        );
        if (res.status !== 200) return this.$message.error(res.msg);
        this.$message({
          message: res.msg,
          type: "success",
          duration: 800,
        });

        sessionStorage.setItem("token", "isLogin");

        setTimeout(() => {
          this.$router.push("/home");
        }, 1000);
      });
    },
    // 重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>

<style scoped>
.login_container {
  height: 100%;
  background-image: radial-gradient(
    circle,
    #1e78ff,
    #3f85ff,
    #5792ff,
    #6b9eff,
    #7fabff,
    #8bb4ff,
    #98bdff,
    #a5c6ff,
    #afcdff,
    #bad4fe,
    #c5dbfe,
    #d0e2fd
  );
}
.login_box {
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 580px;
  height: 350px;
  border-radius: 40px;
  box-shadow: 0 0 20px 3px rgba(0, 0, 0, 0.5);
  background-color: rgba(194, 210, 245, 0.75);
}

.login_form {
  position: absolute;
  top: 35%;
  width: 100%;
  padding: 0 15%;
  box-sizing: border-box;
}

.title {
  width: 100%;
  text-align: center;
  font-size: 30px;
  font-weight: 100;
  text-transform: uppercase;
  color: #414141;
  padding-top: 50px;
  font-family: "Franklin Gothic Medium", "Arial Narrow", Arial, sans-serif;
  text-shadow: 2px 2px 5px #696969;
}

.el-form-item {
  transform: translateX(-5%);
}

.btns {
  display: flex;
  margin-top: 30px;
}

.btn {
  width: 150px;
  border-radius: 14px;
}
</style>
